/**
 * 切换查看模式
 * @param {Object} mode
 */
function changeMode(mode) {
	mMode = mode;
	centerLng=map.getCenter().lng;
	centerLat=map.getCenter().lat;
	zoom=map.getZoom();
	document.getElementById("modeStr").innerText = modeArr[mMode - 1] + "：";
	loadData();
}

/**
 * 加载数据
 */
function loadData() {
	plus.nativeUI.showWaiting("loading",{
		width:"100px",
		height:"100px"
	});
	mui.ajax(FORCAST_DETAIL_DATA, {
		data:{
			type:mMode
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒
		success: function(data) {
			if(data.code == 200) {
				var dArray = data.obj.data;
				map.centerAndZoom(new BMap.Point(centerLng, centerLat), zoom);
				map.clearOverlays();
				for(var i = 0; i < dArray.length; i++) {
					addMarker(dArray[i],dArray[i].Value+modeUnit[mMode-1]);
				}
				plus.nativeUI.closeWaiting();
			}else{
				plus.nativeUI.closeWaiting();
				mui.alert(data.msg);
			}
		},
		error: function(e) {
			plus.nativeUI.closeWaiting();
			mui.alert("服务器连接超时，请稍后再试。")
		}
	});
}

//开启新窗口
function redirectWeather(lng, lat, code, forcastId) {
	mui.openWindow({
		url: 'index.html',
		id: 'weatherPrediction/index.html',
		extras: {
			lng: lng,
			lat: lat,
			code: code,
			forcastId: forcastId
		},
		show: {
			anishow: 'pop-in'
		},
		styles: {
			popGesture: 'close'
		},
		waiting: {
			autoshow: true
		}
	});
}
/**
 * 添加点
 * @param {Object} pointData
 */
function addMarker(pointData,value) {
	var point = new BMap.Point(pointData.lng, pointData.lat);
	var myCompOverlay = new ComplexCustomOverlay(point, pointData,value);
	map.addOverlay(myCompOverlay);
}

//选择模式控件
function ZoomControl() {
	// 默认停靠位置和偏移量
	this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
	this.defaultOffset = new BMap.Size(20, 20); //边距
}
ZoomControl.prototype = new BMap.Control();
ZoomControl.prototype.initialize = function(map) {
	// 创建一个DOM元素
	var select = document.createElement("select");
	// 添加文字说明
	for(var i = 0; i < modeArr.length; i++) {
		var opt = document.createElement("option");
		opt.innerText = modeArr[i];
		opt.setAttribute("value", i + 1);
		select.appendChild(opt);
	}
	// 设置样式
	select.style.width = "80px";
	select.style.background="#5FB878";
	select.style.color="white";
	// 绑定事件
	select.onchange = function(e) {
		changeMode(this.value);
	}
	// 添加DOM元素到地图中
	map.getContainer().appendChild(select);
	// 将DOM元素返回
	return select;
}

function ComplexCustomOverlay(point, forcast,value) {
	this._point = point;
	this._forcast = forcast;
	this._value=value
}
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map) {
	this._map = map;
	var div = this._div = document.createElement("div");
	div.style.position = "absolute";
	div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
	div.style.backgroundColor = "#EE5D5B";
	div.style.color = "white";
	div.style.height = "20px";
	div.style.padding = "2px";
	div.style.lineHeight = "18px";
	div.style.MozUserSelect = "none";
	div.style.border = "none";
	div.style.fontSize = "14px"
	var span = this._span = document.createElement("span");
	div.appendChild(span);
	if(this._value!=null){
		span.appendChild(document.createTextNode(this._forcast.num+":"+this._value));
	}
	var that = this;

	var arrow = this._arrow = document.createElement("div");
	arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
	arrow.style.position = "absolute";
	arrow.style.width = "12px";
	arrow.style.height = "10px";
	arrow.style.top = "20px";
	arrow.style.left = "2px";
	arrow.style.overflow = "hidden";
	div.appendChild(arrow);

	var thatForcast = this._forcast;
	//由于onclick事件只能在pc响应，手机上响应ontouchstart
	div.ontouchstart = function() {
		document.getElementById("forcastCode").innerText = thatForcast.num;
		document.getElementById("forcastNote").innerText = thatForcast.notes;
		document.getElementById("forcastVal").innerText = that._value;
		document.getElementById("redirectWeather").onclick=function(){
			redirectWeather(thatForcast.lng,thatForcast.lat,thatForcast.num,thatForcast.id);
		}
		mui("#popover").popover('toggle', document.getElementById("poppage"));
	}
	map.getPanes().labelPane.appendChild(div);

	return div;
}
ComplexCustomOverlay.prototype.draw = function() {
	var map = this._map;
	var pixel = map.pointToOverlayPixel(this._point);
	this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
	this._div.style.top = pixel.y - 30 + "px";
}